/*
 * @Author: sunkaiyuan 
 * @Date: 2021-10-23 11:05:13 
 * @Last Modified by: sunkaiyuan
 * @Last Modified time: 2021-11-14 15:09:40
 */



(function ($, window, document, undefined) {
  'use strict'
  var Sidebar = function (elem, opts, sidebarmainid) {
    this.elem = $(elem);
    this.sidebarmainid = sidebarmainid;

    this.options = $.extend({}, this.defaults, $.fn.sidebar.defaults, opts);
    this.version = 'v1.0.0';

    this.init(this.options, this.sidebarmainid);

  }

  var vendor = function (opts, sidebarmainid) {

    var li = "<li class='nav-item'></li>",
      ul = "<ul class='nav nav-treeview' style='display: block;'></ul>",
      a = "<a  class=nav-link></a>",
      i = " <i class='nav-icon'></i>",
      p = " <p></p>";
    var html = '';
    if (opts.length > 1)
      opts.sort((a, b) => a.sort - b.sort);
    $.each(opts, (k, v) => {
      var bufa = $(a);
      var bufli = $(li);
      var buful = $(ul);
      var bufi = $(i);
      var bufp = $(p);
      bufli.append(bufa);
      //名称
      bufp.html(v.name);
      //图
      bufi.addClass(v.icon);
      //链接
      bufa.attr("href", v.url == '' ? '#' : v.url);

      bufa.attr("sidebarmainid", sidebarmainid);

      bufa.append(bufi);
      bufa.append(bufp);
      if (v.all_children.length > 0) {
        buful.append(vendor(v.all_children, sidebarmainid));
        bufli.append(buful);
        bufp.append("<i class='right fas fa-angle-left'></i>");
      }
      html += "<li class='nav-item  menu-is-opening menu-open'>" + bufli.html() + "</li>";
    })

    return html;
  }
  Sidebar.prototype = {
    init: function (opts, sidebarmainid) {
      $(this.elem).html("");

      $.each(opts, (k, v) => {
        $(this.elem).append("<li class=isHeaderfist style='padding: .5rem .75rem;'><b>" + v.name + "<b></li>");
        if (v.all_children.length > 0) {
          $(this.elem).append(vendor(v.all_children, sidebarmainid));

        }
      });

    },
    reload: function () {
      this.init(this.options, this.sidebarmainid);
    }
  }
  Sidebar.prototype.funcs = {
    reload: function (opts) {
      $(this.elem).html("");
      $(this.elem).append(vendor(opts));
    },
  }
  $.fn.sidebar = function (options, sidebarmainid) {

    return new Sidebar(this, options, sidebarmainid);
  };
  $.fn.sidebar.defaults = {
  };

})(jQuery, window, document);
var siderbar = [];
var active = 0;
var __loadsidebar = function (id) {
  var elm = $(document).find(".nav-sidebar");
  // function (url, data, success, async, error) 
  if (active == id)
    return;
  else
    active = id;
  if (siderbar[id]) {
    siderbar[id].reload();
  }
  else
    sy.get("/home/sideBarMenu/" + id, {}, function (res) {
      var menu = res[0];
      siderbar[id] = $(elm).sidebar(menu.all_children, id);
      $.exports.SidebarSearch._jQueryInterface.call($("[data-widget=\"sidebar-search\"]"));
    })
}